package com.yangming.controller;

import com.yangming.common.base.BaseController;
import com.yangming.common.base.BaseResponseEnum;
import com.yangming.common.base.Page;
import com.yangming.common.utils.BackJsonBeanUtil;
import com.yangming.model.Supply;
import com.yangming.model.User;
import com.yangming.service.ISupplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 货源Controller
 * @author PengShuiMing
 * @create 2018-09-07
 **/
@RestController
@RequestMapping("/supply")
public class SupplyController extends BaseController {
    private static final Logger logger =   LoggerFactory.getLogger(SupplyController.class);
    @Autowired
    private ISupplyService supplyService;
    /**
     * 发布货源
     */
    @RequestMapping("/saveSupply.json")
    public Object saveSupply(Supply supply) {
        //返回信息
        BaseResponseEnum responseEnum = BaseResponseEnum.ERROR;
        try {
            User user = this.getUser();
            supply.setUserId(user.getId());
            supply.setStatus(1);
            int result = supplyService.saveSupply(supply);
            if(result>0){
                responseEnum = BaseResponseEnum.SUCCESS;
            }
        } catch (Exception e) {
            logger.error("[发布货源]",e.getMessage(),e);
        }
        return BackJsonBeanUtil.getBackJsonBean(responseEnum,null);
    }
    /**
     * 寻找空板车服务
     */
    @RequestMapping("/selectServiceList.json")
    public Object selectEmptyCartServiceList(Supply supply, Page page) {
        //返回信息
        BaseResponseEnum responseEnum = BaseResponseEnum.ERROR;
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            resultMap = supplyService.selectEmptyCartServiceList(supply, page);
            responseEnum = BaseResponseEnum.SUCCESS;
        }catch (Exception e) {
            logger.error("[寻找空板车服务]",e.getMessage(),e);
        }
        return BackJsonBeanUtil.getBackJsonBean(responseEnum,resultMap);
    }
}
